package jmc.ui.front.action;



import java.io.PrintWriter;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jmc.core.service.XMLGeneratorManager;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.DispatchActionSupport;

public class AjaxAction extends DispatchActionSupport{

	public final ActionForward loadInciso(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		log.info("entro a ejecutar el metodo loadInciso");
		
		String selIdInciso = request.getParameter("idInciso");
		String xml = "";

		if(!selIdInciso.equals("")){
			Long idInciso = Long.parseLong(selIdInciso);
			try {
				//IncisoManager incisoManager = (IncisoManager) getWebApplicationContext().getBean("IncisoManager");

				//Inciso inciso = incisoManager.getIncisoByPrimaryKey(idInciso);
				
				//AjaxXmlBuilder ajaxXmlBuilder = new AjaxXmlBuilder();			

				/*xml= ajaxXmlBuilder.addItem("idInciso", String.valueOf(inciso.getIdInciso())).toString();
				xml= ajaxXmlBuilder.addItem("numero", String.valueOf(inciso.getNumero())).toString();
				xml= ajaxXmlBuilder.addItem("cuerpo", inciso.getCuerpo()).toString();
				*/
				XMLGeneratorManager xmlGenerator = (XMLGeneratorManager) getWebApplicationContext().getBean("XMLGeneratorManager");
				Hashtable<String,String> childs = new Hashtable<String,String>();
				//childs.put("idInciso", String.valueOf(inciso.getIdInciso()));
				//childs.put("numero", String.valueOf(inciso.getNumero()));
				//childs.put("cuerpo", inciso.getCuerpo());
				xml = xmlGenerator.generateXML("Inciso", childs);
	
			} catch (Exception ex) {
				// Send back a 500 error code.
				response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Can not create response");
				//ex.printStackTrace();
			return null;
			}
			
		}
		// Set content to xml
		response.setContentType("text/xml; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = response.getWriter();
		pw.write(xml);
		pw.close();
		
		return null;
	}
}
